﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Command
{
   

    #region 原有的类

    public class Cat
    {
        public void Eat(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                Console.WriteLine("小猫啥都没吃。。");
            }
            else
            {
                Console.WriteLine("小猫再吃{0}",str);
            }
        }
    }

    public class Dog
    {
        public void Dining()
        {
            Console.WriteLine("小狗在吃大骨头~~~~");
        }
    }

    #endregion

    public interface IEatCommand
    {
         string FootName { get;}
        void Eat(string str);

        void Redo();
    }

    #region 原有类的的包装

    public class CatCom : IEatCommand
    {
        public Cat cat = null;
        private string footName;
        public CatCom(Cat cat)
        {
            this.cat = cat;
        }

        public string FootName { get { return this.footName; } }

        public void Eat(string str)
        {
            cat.Eat(str);
            this.footName = str;
        }

        public void Redo()
        {
            Eat(this.footName);
        }
    }

    public class DogCom : IEatCommand
    {
        private Dog dog;
        private string footName;
        public DogCom(Dog dog)
        {
            this.dog = dog;
        }

        public string FootName => this.footName;

        public void Eat(string str)
        {
            if (dog!=null)
            {
                dog.Dining(); 
                Console.WriteLine("吃的是：{0}", str);
                this.footName = str;
            }
        }

        public void Redo()
        {
            Eat(this.footName);
        }
    }

    #endregion

    public class ApplicationClass
    {
        public List<IEatCommand> CommandList = new List<IEatCommand>();

        public void  ExcuteCom(IEatCommand com,string foot)
        {
            com.Eat(foot);
            CommandList.Add(com);
        }

        public bool BackExcuteCom()
        {
            int backInt = CommandList.Count - 1-1;
            if (backInt<0)
            {
                if (CommandList.Count>0)
                {
                    CommandList.Clear();
                }
                return false;
            }
            //去除上一步的命令
            CommandList.RemoveAt(CommandList.Count - 1);
            //重复上一条命令动作
            CommandList[CommandList.Count - 1].Redo();
            return true;
        }
     

    }

}
